package com.qijian.plug.capacity.approval;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.qijian.common.core.domain.entity.FlowableDel;
import com.qijian.common.enums.ApprovalDescriptionEnum;
import com.qijian.flowable.domain.Flow;
import com.qijian.flowable.domain.FlowableRecord;
import com.qijian.flowable.service.IFlowService;
import com.qijian.flowable.service.IFlowableRecordService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author qijian
 */
@Component
@RequiredArgsConstructor
public class ApprovalDel extends ApprovalPlugWrapper<FlowableDel.Approval> {

    private final IFlowableRecordService flowableRecordService;





    private final IFlowService flowService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void doSomething(List<Object> args) {
        this.operation(args);
    }

    protected void operation(List<Object> args) {
        if (CollectionUtil.isNotEmpty(args)) {
            // 流程Key
            String processInstanceId = args.get(0).toString();
            FlowableRecord record = flowableRecordService.getById(processInstanceId);
            if (ObjectUtil.isNotNull(record) && ObjectUtil.isNotNull(record.getSourceId()) && !ApprovalDescriptionEnum.C.getValue().equals(record.getStatus())) {
                  if (record.getBusinessKey().startsWith("flow_")) {
                    Flow flow = new Flow();
                    flow.setId(record.getSourceId());
                    flow.setApprovalStatus(ApprovalDescriptionEnum.A);
                    flowService.updateById(flow);
                }
            }
        }
    }
}
